<?php
class item_uploadavatar extends template_item
{
	var $default_name = "Upload Your Avatar";
	var $default_description = "Upload an avatar for you to use. This item will bypass the group setting on uploading avatars.";
	
	var $allow_multiple = 1;
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_ucp');
		$this->ipsclass->load_language('lang_market_items');
		
		$hidden_fields = $this->_get_hidden_fields($inventory_ids);
		
 		list( $this->ipsclass->vars['av_width'], $this->ipsclass->vars['av_height']) = explode ("x", $this->ipsclass->vars['avatar_dims']);
 		$this->ipsclass->lang['av_allowed_files'] = sprintf($this->ipsclass->lang['av_allowed_files'], implode (' .', explode( "|", $this->ipsclass->vars['avatar_ext'] ) ) );
		$this->ipsclass->lang['av_text_url'] = sprintf( $this->ipsclass->lang['av_text_url'], $this->ipsclass->vars['av_width'], $this->ipsclass->vars['av_height'] );
		$this->ipsclass->lang['av_text_upload'] = sprintf( $this->ipsclass->lang['av_text_upload'], $this->ipsclass->vars['avup_size_max'] );
 		
 		if ( $this->ipsclass->vars['allow_flash'] != 1 )
		{
			$this->ipsclass->lang['av_allowed_files'] = str_replace( ".swf", "", $this->ipsclass->lang['av_allowed_files'] );
		}
		
		$size_html = "";
		if($this->ipsclass->vars['disable_ipbsize'])
		{
			$size_html = <<<EOF
	<tr>
		<td class='row1' align='left'>
			&nbsp;
		</td>
		<td class='row2' align='right'>
			<b>{$this->ipsclass->lang['width']}</b>
			&nbsp;
			<input type='text' size='3" name='man_width' />
			&nbsp;&nbsp;
			<b>{$this->ipsclass->lang['height']}</b>
			&nbsp;
			<input type='text' size='3' name='man_height' /></td>
	</tr>
EOF;
		}
		else
		{
			$size_html = <<<EOF
	<tr>
		<td class='row1' align='left'>
			&nbsp;
		</td>
		<td class='row2' align='left'>
			<b>{$this->ipsclass->lang['av_autosize']}</b>
			<br />
			({$this->ipsclass->lang['av_as_desc']})
		</td>
	</tr>
EOF;
		}
		
		return <<<EOF
<div class='formsubtitle'>{$this->ipsclass->lang['upload_avatar']}</div>
<form action='{$this->base_url}' method='post' enctype='multipart/form-data'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left' colspan='2'>
				{$this->ipsclass->lang['av_text_url']} {$this->ipsclass->lang['av_text_upload']}
				<br />
				{$this->ipsclass->lang['av_allowed_files']}
			</td>
		</tr>
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['select_an_image_file']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='file' size='30' name='upload_avatar' />
			</td>
		</tr>
{$size_html}
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' name='item_used' value='{$this->ipsclass->lang['upload_avatar']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
EOF;
	}
	
	function input_done()
	{
		return isset($this->ipsclass->input['item_used']) && isset($_FILES['upload_avatar']) && is_uploaded_file($_FILES['upload_avatar']['tmp_name']);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		require ROOT_PATH."sources/lib/func_usercp.php";
		$ucp = new func_usercp();
		$ucp->ipsclass = &$this->ipsclass;
		
		require_once(KERNEL_PATH."class_upload.php");
		$upload = new class_upload();
		$upload->ipsclass = &$this->ipsclass;
		
		require_once( KERNEL_PATH.'class_image.php' );
		$image = new class_image();
		$image->ipsclass = &$this->ipsclass;
		
		$exts = array();
		foreach(explode( ",", $this->ipsclass->vars['avatar_ext'] ) as $ext)
		{
			$exts[] = trim($ext);
		}
		
		//list($p_width, $p_height) = explode( "x", strtolower($this->ipsclass->vars['avatar_dims']) );
		
		if ( preg_match( "#javascript:#is", $_FILES['upload_avatar']['name'] ) )
		{
			$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'no_av_upload' ) );
		}
		
		$upload->out_file_name = 'av-'.$this->ipsclass->member['id'];
		$upload->out_file_dir = $this->ipsclass->vars['upload_dir'];
		$upload->max_file_size = ($this->ipsclass->vars['avup_size_max'] * 1024) * 8;
		$upload->allowed_file_ext = $exts;
		$upload->upload_form_field = 'upload_avatar';
		
		$ucp->bash_uploaded_avatars($this->ipsclass->member['id']);
		
		$upload->upload_process();
		
		if($upload->error_no)
		{
			$this->ipsclass->DB->do_update( 'member_extra', array( 'avatar_location' => '', 'avatar_size' => '', 'avatar_type' => '' ), 'id='.$this->ipsclass->member['id'] );
			
			switch( $upload->error_no )
			{
				case 1:
					$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'upload_failed' ) );
				case 2:
					$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'no_av_type' ) );
				case 3:
					$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'upload_to_big') );
				case 4:
					$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'upload_failed' ) );
				case 5:
					$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'upload_failed' ) );
			}
		}
		
		list($p_width, $p_height) = explode( "x", strtolower($this->ipsclass->vars['avatar_dims']) );
		
		$real_name = $upload->parsed_file_name;
		
		if ( ! $this->ipsclass->vars['disable_ipbsize'] and $upload->file_extension != '.swf' )
		{
			$image->in_type        = 'file';
			$image->out_type       = 'file';
			$image->in_file_dir    = $this->ipsclass->vars['upload_dir'];
			$image->in_file_name   = $real_name;
			$image->out_file_name  = 'avs-'.$this->ipsclass->member['id'];
			$image->desired_width  = $p_width;
			$image->desired_height = $p_height;
			
			$return = $image->generate_thumbnail();

			$im['img_width']  = $return['thumb_width'];
			$im['img_height'] = $return['thumb_height'];
			
			if ( isset($return['thumb_location']) AND strstr( $return['thumb_location'], 'avs-' ) )
			{
				@unlink( $this->ipsclass->vars['upload_dir']."/".$real_name );
				
				$real_name = 'av-'.$this->ipsclass->member['id'].'.'.$this->image->file_extension;
				
				@rename( $this->ipsclass->vars['upload_dir']."/".$return['thumb_location'], $this->ipsclass->vars['upload_dir']."/".$real_name );
				@chmod(  $this->ipsclass->vars['upload_dir']."/".$real_name, 0777 );
			}
		}
		else
		{	
			$w = intval($this->ipsclass->input['man_width'])  ? intval($this->ipsclass->input['man_width'])  : $p_width;
			$h = intval($this->ipsclass->input['man_height']) ? intval($this->ipsclass->input['man_height']) : $p_height;
			$im['img_width']  = $w > $p_width  ? $p_width  : $w;
			$im['img_height'] = $h > $p_height ? $p_height : $h;
		}
		
		if ( filesize( $this->ipsclass->vars['upload_dir']."/".$real_name ) > ($this->ipsclass->vars['avup_size_max']*1024))
		{
			@unlink( $this->ipsclass->vars['upload_dir']."/".$real_name );
			
			$this->ipsclass->DB->do_update( 'member_extra', array( 'avatar_location' => '', 'avatar_size' => '', 'avatar_type' => '' ), 'id='.$this->ipsclass->member['id'] );
			
			$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'upload_to_big' ) );
		}
		
		$this->ipsclass->DB->do_update( 'member_extra', array( 'avatar_location' => $real_name, 'avatar_size' => $im['img_width'].'x'.$im['img_height'], 'avatar_type' => 'upload' ), 'id='.$this->ipsclass->member['id'] );
		
		$this->delete_items($inventory_ids);
		
		$inv_id = reset($inventory_ids);
		$this->_write_log($inv_id, "Uploaded avatar");
		
		$this->ipsclass->market_class->redirect('avatar_updated',$this->ipsclass->base_url.'act=UserCP&CODE=24',array('url'=>1));
	}
}
?>